package com.lockapp.services;

import java.util.List;

import com.lockapp.PrincipalActivity;
import com.lockapp.SenhaActivity;
import com.lockapp.database.BlockCursor;
import com.lockapp.database.BlockDAO;
import com.lockapp.entity.Block;

import android.app.ActivityGroup;
import android.app.ActivityManager;
import android.app.Service;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Parcel;
import android.util.Log;
import android.widget.Toast;

public class LockService extends Service{

	private Handler handler;
	private Thread thread;
	private Runnable runnable;
	boolean running;
	
	boolean bloqueado = true;
	
	private BlockDAO dao;
	private BlockCursor cursor;
	private Block blockGlobal;
	private Block blockGlobalLockApp;
	private String checaPacote = "";
	private String checaClasse = "";
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		
		dao = new BlockDAO(getApplicationContext());
		cursor = dao.getAll();
		
		running = true;
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				cursor = dao.getAll();
				ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
				List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(30);
				System.out.println(tasks.get(0).baseActivity.flattenToShortString());
				if(!checaClasse.equals(tasks.get(0).baseActivity.flattenToShortString()) && !bloqueado){
					blockGlobalLockApp.setLiberacao(0);
					dao.alterar(blockGlobalLockApp);
					bloqueado = true;
				}
				if(tasks.get(0).baseActivity.flattenToShortString().equals("com.lockapp/.PrincipalActivity") && cursor.getCount()!= 0){
					//if(!checaClasse.equals(tasks.get(0).baseActivity.flattenToShortString()) && !bloqueado){
					//	blockGlobalLockApp.setLiberacao(0);
					//	dao.alterar(blockGlobalLockApp);
					//	bloqueado = true;
					//}
					
					for(int i = 0; i < cursor.getCount(); i++){
						cursor.moveToPosition(i);

						if(cursor.getPacoteApp().equals(tasks.get(0).baseActivity.flattenToShortString()) && cursor.getLiberacao() == 0 && bloqueado){
							checaClasse = tasks.get(0).baseActivity.flattenToShortString();
							//pega somente o id do block para alterar
							bloqueado = false;
							blockGlobalLockApp = new Block();
							blockGlobalLockApp.setIdBlock(cursor.getIdBlock());
							Intent intent = new Intent(getApplicationContext(), SenhaActivity.class);
							intent.putExtra("block", cursor.getIdBlock());
							intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivity(intent);

							//cursor.close();
							//dao.close();
						}
					}
			
				}else{
				
					if((!checaPacote.equals(tasks.get(0).baseActivity.getPackageName())) && (blockGlobal != null) && (!tasks.get(0).baseActivity.getPackageName().equals("com.lockapp"))){
						blockGlobal.setLiberacao(0);
						dao.alterar(blockGlobal);
					}

					for(int i = 0; i < cursor.getCount(); i++){
						cursor.moveToPosition(i);
						if(cursor.getPacoteApp().equals(tasks.get(0).baseActivity.getPackageName()) && cursor.getLiberacao() == 0){
							checaPacote = tasks.get(0).baseActivity.getPackageName();
							blockGlobal = new Block();
							blockGlobal.setIdBlock(cursor.getIdBlock());

							Intent intent = new Intent(getApplicationContext(), SenhaActivity.class);
							intent.putExtra("block", cursor.getIdBlock());
							intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivity(intent);
							//							cursor.close();
							//							dao.close();
						}

					}

					//				for(ActivityManager.RunningTaskInfo info : tasks){
					//					Log.i("RODANDO", info.baseActivity.getPackageName());
					//				}
				}
			}
		};

		runnable = new Runnable() {
			
			@Override
			public void run() {
				 while(running){
		           try {
		            Thread.sleep(500);
		            handler.sendEmptyMessage(0);
	
			        } catch (InterruptedException e) {
			            e.printStackTrace();
			        } 
		        }
			}
		};
		
		thread = new Thread(runnable);
		thread.start();
	}
	
	@Override
	public void onDestroy() {
		running = false;
	}
	
}
